/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.liquidByte.trafficMustang.server.webService;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.liquidByte.trafficMustang.server.dataModel.Deslocamento;
import org.liquidByte.trafficMustang.server.dataModel.Localizacao;
import org.liquidByte.trafficMustang.server.dataModel.Trecho;
import org.liquidByte.trafficMustang.server.services.ConverterService;
import org.liquidByte.trafficMustang.server.services.TrechoService;
import org.liquidByte.trafficMustang.server.webService.base.WebService;
import org.liquidByte.trafficMustang.server.webService.dto.DeslocamentoDTO;
import org.liquidByte.trafficMustang.server.webService.dto.TrechoDTO;
import org.liquidByte.trafficMustang.server.webService.dto.TrechoLocalizacaoDTO;

/**
 * REST Web Service
 *
 * @author Alexandre
 */
@Stateless
@Path("trecho")
public class TrechoWebService extends WebService {

    @EJB
    private TrechoService trechoService;
    @EJB
    private ConverterService converter;
    
    @POST
    @Consumes("application/json")
    @Produces("application/json")
    @Path("requisitar")
    public String requisitar(String data) {
        try {
            DeslocamentoDTO dto = this.toObject(data, DeslocamentoDTO.class);
            Deslocamento deslc = this.converter.getDeslocamentoDto().convert(dto);
            this.trechoService.requisitar(deslc);
            return this.success();
        } catch (Exception e) {
            return this.error(e);
        }
    }
    
    @POST
    @Consumes("application/json")
    @Produces("application/json")
    @Path("get")
    public String get(String data) {
        try {
            DeslocamentoDTO dto = this.toObject(data, DeslocamentoDTO.class);
            Deslocamento deslc = this.converter.getDeslocamentoDto().convert(dto);
            return this.result(this.converter.getTrecho().convert(deslc.getTrechoList()));
        } catch (Exception e) {
            return this.error(e);
        }
    }
    
    @POST
    @Consumes("application/json")
    @Produces("application/json")
    @Path("confirmar")
    public String confirmar(String data) {
        try {
            TrechoDTO dto = this.toObject(data, TrechoDTO.class);
            Trecho trecho = this.converter.getTrechoDto().convert(dto);
            this.trechoService.confirmar(trecho);
            return this.success();
        } catch (Exception e) {
            return this.error(e);
        }
    }
    
    @POST
    @Consumes("application/json")
    @Produces("application/json")
    @Path("negar")
    public String negar(String data) {
        try {
            TrechoDTO dto = this.toObject(data, TrechoDTO.class);
            Trecho trecho = this.converter.getTrechoDto().convert(dto);
            this.trechoService.negar(trecho);
            return this.success();
        } catch (Exception e) {
            return this.error(e);
        }
    }
    
    @POST
    @Consumes("application/json")
    @Produces("application/json")
    @Path("cancelar")
    public String cancelar(String data) {
        try {
            TrechoDTO dto = this.toObject(data, TrechoDTO.class);
            Trecho trecho = this.converter.getTrechoDto().convert(dto);
            this.trechoService.cancelar(trecho);
            return this.success();
        } catch (Exception e) {
            return this.error(e);
        }
    }
    
    @POST
    @Consumes("application/json")
    @Produces("application/json")
    @Path("embarque")
    public String embarque(String data) {
        try {
            TrechoLocalizacaoDTO dto = this.toObject(data, TrechoLocalizacaoDTO.class);
            Localizacao locl = this.converter.getLocalizacaoDto().convert(dto.getLocalizacao());
            Trecho trecho = this.converter.getTrechoDto().convert(dto.getTrecho());
            
            this.trechoService.embarque(locl, trecho);
            return this.success();
        } catch (Exception e) {
            return this.error(e);
        }
    }
    
    @POST
    @Consumes("application/json")
    @Produces("application/json")
    @Path("desembarque")
    public String desembarque(String data) {
        try {
            TrechoLocalizacaoDTO dto = this.toObject(data, TrechoLocalizacaoDTO.class);
            Localizacao locl = this.converter.getLocalizacaoDto().convert(dto.getLocalizacao());
            Trecho trecho = this.converter.getTrechoDto().convert(dto.getTrecho());
            
            this.trechoService.desembarque(locl, trecho);
            return this.success();
        } catch (Exception e) {
            return this.error(e);
        }
    }
}
